/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MainPack;

import MaterialPack.ConveyorBelt;
import MaterialPack.InfoBar;
import MaterialPack.MaterialObject;
import MaterialPack.PetInstance;
import MaterialPack.PetItem;
import Supporter.ImageSupporter;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Graphics;
import EntityGameObject.*;
import MaterialPack.Button;
import MaterialPack.ContextDialog;
import MaterialPack.InfoPanel;
import javax.microedition.media.MediaException;
import javax.microedition.lcdui.game.Sprite;
import javax.obex.PasswordAuthentication;
/**
 *
 * @author Van persie
 */
public class PlayPetCanvas extends AbstractCanvas{
    ConveyorBelt listItem = null;
    PetInstance pet = null;
    
    InfoPanel LVLInfo = null;
    InfoBar EXPbar = null;
    InfoBar Statusbar = null;
    InfoBar Emotionbar = null;
    InfoPanel EXPInfo = null;
       
    InfoPanel GuidePanel = null;
    Button BackBtn = null;    
    
    private Timer timer ;        
    private Sticker sticker ;
    private int clock = 0;
    public PlayPetCanvas(MainMIDlet midlet)
    { 
        
        main = midlet;
        height = this.getHeight();
        width = this.getWidth();
        
        
        if(listObjects != null)
            listObjects.removeAllElements();
        listObjects = new Vector();
        
        try{
            if(height != width)
                LoadResource( );            
        }catch(Exception e)
        {
            
        }
        
        
        
    }
    private void StartTimer() {
        sticker = new Sticker();        
        timer.schedule(sticker, 50,50);
        
    }
    public void PauseTimer()
    {
        sticker.cancel();
    }
    
        
    class Sticker extends TimerTask
        {
            public void run()
            {
                Update();
                repaint();
                
            }
        }
    
    public void LoadResource() throws IOException {     
       
        ImageSupporter imgSupporter = new ImageSupporter();
        Image temp = imgSupporter.getImage("/scene-base.jpg");
        background = ImageSupporter.scaleImage(temp, width, height);
                
        BackBtn = new Button("/Return.png", null, width/12, height-width/6, width/6, width/6);
        
        Image foodBar = ImageSupporter.alphaBlend(imgSupporter.getImage("/scene-food-bar.png"));
        
        //listItem = new ConveyorBelt(foodBar, null, 0, 2*height/3, width, height/3);
        //GuidePanel = new InfoPanel(imgSupporter.getImage("/scene-food-bar.png"),"", width/3, 0, 2*width/3, height/6,Font.getFont(Font.FACE_SYSTEM, Font.FONT_STATIC_TEXT, Font.SIZE_MEDIUM));
        listItem = new ConveyorBelt(imgSupporter.getImage("/scene-foodbar.png"), null, 0, 2*height/3, width + width/12, height/3);
        GuidePanel = new InfoPanel(ImageSupporter.transparent(foodBar),"", width/3, 0, 2*width/3, height/6,Font.getFont(Font.FACE_SYSTEM, Font.FONT_STATIC_TEXT, Font.SIZE_SMALL));
        GuidePanel.setAnchorMode(InfoPanel.ANC_MIDDLE);
        listObjects.addElement(GuidePanel);
        
        LoadPetInstance();
        LoadConveyorBelt();    
        timer = new Timer();
        StartTimer();
    }



    public void LoadPetInstance()
    {
        
        int scalar = width/6;
        pet = new PetInstance(width/2, 2*height/3, 2*width/3, 2*width/3,main.getPet(),true);
        pet.setOutput(GuidePanel); 
        LVLInfo = new InfoPanel(null, "Level " + Integer.toString(pet.GetAttributes(Pet.LEVEL)), 5, 5, scalar, 2*scalar/3, Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_LARGE));
        LVLInfo.setNoBackground(true);
        LVLInfo.setAnchorMode(InfoPanel.ANC_LEFT);
        EXPInfo = new InfoPanel(null, "EXP ", 5, 2*width/3 + 25, scalar , 2*scalar/3, Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_MEDIUM));
        EXPInfo.setNoBackground(true);
        EXPInfo.setAnchorMode(InfoPanel.ANC_LEFT);
        //EXPbar = new InfoBar("/option-staminabar.png","/option-staminabar-mask.png", InfoBar.HORIZON, pet.GetAttributes(Pet.EXP), 5 + scalar, 2*width/3 + 25, 3*width/4, 8);
        
        EXPbar = new InfoBar("/option-expbar.png","/option-expbar-mask.png", InfoBar.HORIZON, pet.GetAttributes(Pet.EXP), 5 + scalar, 2*width/3 + 25, 175, 14);
        
        EXPbar.SetColor(128, 128, 50);
        Statusbar = new InfoBar("/option-staminabar.png","/option-staminabar-mask.png", InfoBar.VERTICAL, 100*pet.getStatus()/pet.getMaxStatus(), width - scalar/3-10, 2*scalar, scalar/4, height/2);
        InfoPanel temp1 = new InfoPanel(null, "Stt", width - scalar/3 - 10,4*scalar/3   , scalar/3, scalar/3,Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_SMALL));
        
        Emotionbar = new InfoBar("/option-staminabar.png","/option-staminabar-mask.png", InfoBar.VERTICAL,100*pet.getEmotion()/pet.getMaxEmotion(),width - scalar- 10, 2*scalar, scalar/4, height/2);
        InfoPanel temp2 = new InfoPanel(null, "Emo", width - scalar - 10,4*scalar/3   , scalar/3, scalar/3,Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_SMALL));
        
       
        listObjects.addElement(pet);
        listObjects.addElement(LVLInfo);
        listObjects.addElement(EXPInfo);
        listObjects.addElement(EXPbar);
        listObjects.addElement(Statusbar);
        listObjects.addElement(Emotionbar);
        listObjects.addElement(temp1);
        listObjects.addElement(temp2);
        
    }
    protected void sizeChanged(int w, int h)
    {
        width = w;
        height = h;
        try{
            LoadResource( );            
        }catch(Exception e){}
        
    }
    public void LoadConveyorBelt()
    {

        
        listItem.RemoveAll();
        
            
        for(int i =0 ; i < pet.getListItem().size(); i++)
        {
            listItem.Add(new PetItem((PetItem)pet.getListItem().elementAt(i),PetItem.TYPE_DRAG));
        }

        listItem.ArrangeItem();       
        
        listObjects.addElement(listItem);
        listItem.setRun(true);
       
        
    }
    protected void keyReleased(int key)
    {
        switch(key)
        {            
            case KEY_NUM0:
                main.exitMIDlet();
                break;
            case KEY_NUM1:
                //listItem.setRun(true);
                //StartTimer();
                break;
            case KEY_NUM2:
                PauseTimer();
                break;
        }
        repaint();
    }
    protected void pointerDragged(int x, int y)
    {
         for(int i=0; i <listObjects.size(); i++)
         {
             ((MaterialObject)listObjects.elementAt(i)).Drag(x, y);             
         
         }
         repaint();
    }

    protected void pointerPressed(int x, int y)
    {
         for(int i=0; i <listObjects.size(); i++)
             ((MaterialObject)listObjects.elementAt(i)).Press(x,y);
         BackBtn.Press(x, y);    
         repaint();
    }
    protected void pointerReleased (int x, int y)
    {
        if(listItem.getCurrDrag() != null && pet.isContained(x, y))
        {
                        
            pet.Feed(listItem.getCurrDrag());
            listItem.getListItem().removeElement(listItem.getCurrDrag());
            listItem.ArrangeItem();
            // tinh ti le exp tang len
            pet.AddAttributes(Pet.EXP,listItem.getCurrDrag().getIncrEXP());
            EXPbar.setRatio(pet.GetAttributes(Pet.EXP));
        }
        for(int i=0; i <listObjects.size(); i++)
             ((MaterialObject)listObjects.elementAt(i)).Release(x,y);
        
        if(BackBtn.Release(x, y))  
        {
            PauseTimer();
                try {
                    main.SwitchTo(MainMIDlet.SCENE_MANAGEMENT);
                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (MediaException ex) {
                    ex.printStackTrace();
                }
            
        }
        
  
        repaint();
    }
    public void Update()
    {
        listItem.Update();
        pet.Update();
            
        Statusbar.setRatio(100*pet.getStatus()/pet.getMaxStatus());
        Emotionbar.setRatio(100*pet.getEmotion()/pet.getMaxEmotion());
        EXPbar.setRatio(pet.GetAttributes(Pet.EXP));
        LVLInfo.setText("Level " + Integer.toString(pet.GetAttributes(Pet.LEVEL)));
        clock = (clock + 1)%10;
        repaint();
    }
    public void paint(Graphics g )
    {
        g.drawImage(background,  0, 0, Graphics.TOP | Graphics.LEFT);
        for(int i=0; i <listObjects.size(); i++)
            ((MaterialObject)listObjects.elementAt(i)).paint(g);
        
        BackBtn.paint(g);
    }
}
